﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using CodeStash.Common.DTOS;
using CodeStash.Common.Encryption;
using CodeStash.Common.Enums;

namespace RESTAPI_Test_Client
{
    internal class RESTAPIAddSnippetRunner : RESTAPIRunnerBase
    {

        internal override void RunSpecificRunnerAPILogic()
        {
            AddSnippet_ExistingCategory_NoGrouping_WithoutAnyTags();
            AddSnippet_NewCategory_NoGrouping_WithoutAnyTags();
            AddSnippet_ExistingCategory_WithGrouping_WithoutAnyTags();
            AddSnippet_ExistingCategory_NoGrouping_WithSingleTag();
            AddSnippet_ExistingCategory_NoGrouping_WithSeveralTags();
        }


        private void AddSnippet_ExistingCategory_NoGrouping_WithoutAnyTags()
        {
            JSONCodeSnippet snippetToAdd = new JSONCodeSnippet(
                GetCSharpSnippet(),
                1,
                "C#", //use existing C# category
                3,//C# is always 3
                "C#",
                "", //no tags
                "A test codeSnippet using existing Category",
                "A test codeSnippet using existing Category",
                null,
                "", //No grouping
                CodeSnippetVisibility.Public,0,0,0);

            AddSnippetToWebSite(snippetToAdd, "AddSnippet_ExistingCategory_NoGrouping_WithoutAnyTags");
        }


        private void AddSnippet_NewCategory_NoGrouping_WithoutAnyTags()
        {
            JSONCodeSnippet snippetToAdd = new JSONCodeSnippet(
                GetCSharpSnippet(),
                0,
                "Category_" + Guid.NewGuid().ToString(), //New category
                3,//C# is always 3
                "C#",
                "", //no tags
                "A test codeSnippet using existing Category",
                "A test codeSnippet using existing Category",
                null,
                "", //No grouping
                CodeSnippetVisibility.Public,0,0,0);

            AddSnippetToWebSite(snippetToAdd, "AddSnippet_NewCategory_NoGrouping_WithoutAnyTags");
        }


        private void AddSnippet_ExistingCategory_WithGrouping_WithoutAnyTags()
        {
            JSONCodeSnippet snippetToAdd = new JSONCodeSnippet(
                GetCSharpSnippet(),
                1,
                "C#", //use existing C# category
                3,//C# is always 3
                "C#",
                "", //no tags
                "A test codeSnippet using existing Category",
                "A test codeSnippet using existing Category",
                null,
                "group_" + Guid.NewGuid().ToString(), //New grouping
                CodeSnippetVisibility.Public,0,0,0);

            AddSnippetToWebSite(snippetToAdd, "AddSnippet_ExistingCategory_WithGrouping_WithoutAnyTags");
        }

        private void AddSnippet_ExistingCategory_NoGrouping_WithSingleTag()
        {
            JSONCodeSnippet snippetToAdd = new JSONCodeSnippet(
                GetCSharpSnippet(),
                1,
                "C#", //use existing C# category
                3,//C# is always 3
                "C#",
                "Tag_" + Guid.NewGuid().ToString(), //single tag
                "A test codeSnippet using existing Category",
                "A test codeSnippet using existing Category",
                null,
                "", //No grouping
                CodeSnippetVisibility.Public,0,0,0);

            AddSnippetToWebSite(snippetToAdd, "AddSnippet_ExistingCategory_NoGrouping_WithSingleTag");
        }

        private void AddSnippet_ExistingCategory_NoGrouping_WithSeveralTags()
        {
            JSONCodeSnippet snippetToAdd = new JSONCodeSnippet(
                GetCSharpSnippet(),
                1,
                "C#", //use existing C# category
                3,//C# is always 3
                "C#",
                "Tag_" + Guid.NewGuid().ToString() + ";" + "Tag_" + Guid.NewGuid().ToString(), //serveral tags
                "A test codeSnippet using existing Category",
                "A test codeSnippet using existing Category",
                null,
                "", //No grouping
                CodeSnippetVisibility.Public,0,0,0);

            AddSnippetToWebSite(snippetToAdd, "AddSnippet_ExistingCategory_NoGrouping_WithSingleTag");
        }


        private void AddSnippetToWebSite(JSONCodeSnippet snippetToAdd, string methodName)
        {

            //REST controller Action is : AddSnippet(JSONAddSnippetInput input)
            //Encrypt using same rules as website, see App.Config, and CodeStash Web.Config
            values = new NameValueCollection();

            JSONAddSnippetInput input = new JSONAddSnippetInput(
                                "", //Encrypted using same rules as website
                                base.emailForWebsite, //Encrypted using same rules as website
                                base.passwordForWebsite, //Encrypted using same rules as website
                                snippetToAdd);

            Utilities.AddValue(values, "input", input);

            Byte[] results = client.UploadValues("http://localhost:8300/Rest/AddSnippet", values);

            JSONCodeSnippetAddSingleResult addedSnippet = Utilities.GetValue<JSONCodeSnippetAddSingleResult>(results);
            if (addedSnippet.CodeSnippet != null)
                Console.WriteLine(String.Format("{0} : Snippet Sucessfully Added With Id : {1}",
                    methodName, addedSnippet.CodeSnippet.CodeSnippetId));
            else
                Console.WriteLine(addedSnippet.ErrorMessage);
        }


        private String GetCSharpSnippet()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("foreach(string s in GetAllStrings())");
            sb.AppendLine("{");
            sb.AppendLine("    //This should print to console");
            sb.AppendLine("    Console.WriteLine(s);");
            sb.AppendLine("}");
            return sb.ToString();

        }





    }
}
